// pages/home/home.js
import {getAllBookType,getBookList} from '../../../api/apiUrl'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    barList: [],
    total:0,
    bookList:[],
    parms:{
      "bookName": "",
      "pageNow": 1,
      "pageSize":10,
      "type": ""
    },
    scrollTop: 0,
    old: {
      scrollTop: 0
    },
    activeIndex:0,
    toView:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      bookList:[]
    })
    this.init()
  },
  // 点击事件
	init() {
    this.getList()
    // 获取书籍类别
    getAllBookType({
      "name": "",
      "pageNow": 1,
      "pageSize": 100
    }).then(res=>{
      let obj = [{
        id: "",
        name: "全部"
      }]
      this.setData({
        barList:[...obj,...res]
      })
    })
  },
  clickBarItem(e){
    console.log(e);
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    this.setData({
      activeIndex: index,
      toView:'viewid'+(index-2),
      bookList:[],
      'parms.pageNow':1
    });
    this.getList(id)
  },
  async getList(id=''){
    let obj = this.data.parms
    let list = this.data.bookList
    let res = await getBookList({...obj,type:id})
    if(res.list.length){
      res.list.map(v => {
        v.imgShowList = v.picList ? v.picList.split(",") : [];
        v.imgShow =  v.picList ? v.picList.split(",")[0] : '';
      })
    }
    console.log(res);
    this.setData({
      total:res.total,
      bookList:list.concat(res.list)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let {total,bookList} = this.data
    if(bookList.length < total){
        this.data.parms.pageNow++
        this.getList()
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})